package com.gnerv.business.platform.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gnerv.business.platform.common.response.R;
import com.gnerv.business.platform.common.util.UUIDUtil;
import com.gnerv.business.platform.entity.Role;
import com.gnerv.business.platform.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: Gnerv LiGen
 * @date: 2018-07-03
 **/
@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/platform/role")
public class RoleController implements BaseController<Role> {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "创建角色", notes = "角色用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pId", value = "上级角色Id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "角色名", required = true, paramType = "query")
    })
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Override
    public R create(Role role) {
        role.setRoleId(UUIDUtil.getLowerUUID());
        boolean b = roleService.insert(role);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "删除角色", notes = "根据角色id 删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.GET)
    @Override
    public R delete(@PathVariable(value = "roleId") String roleId) {
        Role role = new Role();
        role.setRoleId(roleId);
        boolean b = roleService.delete(new EntityWrapper<>(role));
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "更新角色", notes = "根据角色id 更新角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名", required = true, paramType = "query")
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Override
    public R update(Role role) {
        boolean b = roleService.updateByRoleId(role);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询单个角色", notes = "根据角色id 查询单个角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/select/{roleId}", method = RequestMethod.GET)
    @Override
    public R select(@PathVariable(value = "roleId") String roleId) {
        Role role = new Role();
        role.setRoleId(roleId);
        role = roleService.selectOne(new EntityWrapper<>(role));
        return role != null ? R.ok(R.OK_200).data("role", role) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询角色列表", notes = "根据条件 查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名", paramType = "query")
    })
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Override
    public R list(Role role) {
        List<Role> roles = roleService.selectList(new EntityWrapper<>(role));
        return roles != null ? R.ok(R.OK_200).data("roles", roles) : R.error(R.ERROR_500);
    }
}
